//
//  CVTypeButton.m
//  eConsume
//
//  Created by haoyi on 14-3-5.
//  Copyright (c) 2014年 Hooy1. All rights reserved.
//

#import "CVTypeButton.h"
#import "UIImageView+WebCache.h"
@implementation CVTypeButton

static const CGFloat g_fOrigin_X               = 5.0;
static const CGFloat g_fHeight_Image           = 30.0;
static const CGFloat g_fHeight_Title           = 15.0;
static const CGFloat g_fDistance               = 0.0;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        _typeLogIMV = [[UIImageView alloc] initWithFrame:
                       CGRectMake(g_fOrigin_X,
                                  (self.frame.size.height - g_fHeight_Image - g_fHeight_Title - g_fDistance) / 2,
                                  (self.frame.size.width - g_fOrigin_X * 2), g_fHeight_Image)];
        _typeLogIMV.backgroundColor = [UIColor clearColor];
        _typeLogIMV.contentMode = UIViewContentModeScaleAspectFit;
        _typeLogIMV.clipsToBounds = YES;
        [self addSubview:_typeLogIMV];
        
        _titleTabel = [[UILabel alloc] initWithFrame:
                       CGRectMake(0,
                                  _typeLogIMV.frame.origin.y +
                                  _typeLogIMV.frame.size.height + g_fDistance,
                                  self.frame.size.width,
                                  g_fHeight_Title)];
        _titleTabel.backgroundColor = [UIColor clearColor];
        _titleTabel.textAlignment = 1;
        [self addSubview:_titleTabel];
    }
    
    return self;
}

-(void)setTitleFontWithfont:(UIFont *)font{

    _titleTabel.font = font;
}

-(void)setTitleColorWithcolor:(UIColor *)color{

    _titleTabel.textColor = color;
}

-(void)setContentWithDict:(NSDictionary *)dict{

    UIImage *tempImage = [UIImage imageNamed:[dict objectForKey:str_Key_imageName]];

    if (self.frame.size.height < g_fHeight_Title) {
    
        _typeLogIMV.frame = CGRectMake(0, 0, 0, 0);
    }else if (tempImage.size.height <= g_fHeight_Image) {
        
        _typeLogIMV.frame = CGRectMake(g_fOrigin_X,
                                       (self.frame.size.height - g_fHeight_Image - g_fHeight_Title - g_fDistance) / 2,
                                       (self.frame.size.width - g_fOrigin_X * 2), g_fHeight_Image);
    }
    
    _titleTabel.frame = CGRectMake(0,
                                   _typeLogIMV.frame.origin.y +
                                   _typeLogIMV.frame.size.height + g_fDistance,
                                   self.frame.size.width,
                                   g_fHeight_Title);
    
    if ([[dict objectForKey:str_Key_imageName] hasPrefix:@"http://"] ||
        [[dict objectForKey:str_Key_imageName] isEqualToString:@""]) {
        
        [_typeLogIMV setImageWithURL:[NSURL URLWithString:[dict objectForKey:str_Key_imageName]]
                    placeholderImage:[UIImage imageNamed:@"img_default_square"]];
    }else{
    
        [_typeLogIMV setImage:[UIImage imageNamed:[dict objectForKey:str_Key_imageName]]];
    }

    _titleTabel.text = [dict objectForKey:str_Key_typeName];
}

@end
